<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <script type="text/javascript" th:src="@{/jquery.3.3.1.min.js}"></script>
    <script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.js}"></script>
    <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.css}" />
    <script type="text/javascript" th:src="@{/jquery.md5.js}"></script>
    <script type="text/javascript" th:src="@{/jquery.validate.js}"></script>
    <script type="text/javascript" th:src="@{/layer.js}"></script>
</head>
<body>
    <form name="loginForm" id="loginForm" method="post" style="width: 50%; margin: 0 auto">
        <h2 style="text-align: center; margin-bottom: 20px">Login</h2>
        <div class="form-group">
            <div class="row">
                <label class="form-label col-md-4">Telephone</label>
                <div class="col-md-6">
                    <input id="mobile" name="mobile" class="form-control" type="text"
                           placeholder="telephone..." required="required" minlength="11" maxlength="11" />
                </div>
                <div class="col-md-1"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <label class="form-label col-md-4">Password</label>
                <div class="col-md-6">
                    <input id="password" name="password" class="form-control" type="password"
                        placeholder="password..." required="required" minlength="6" maxlength="16"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-3">
                    <button class="btn btn-primary btn-block" type="reset" onclick="reset()">reset</button>
                </div>
                <div class="col-md-3">
                    <button class="btn btn-primary btn-block" type="submit" onclick="login()">login</button>
                </div>
            </div>
        </div>
    </form>
<script type="text/javascript">
    function login() {
        $("#loginForm").validate({
           submitHandler: function(form) {
               doLogin();
           }
        });
    }

    function doLogin() {
        var inputPass = $("#password").val();
        var salt = '1a2b3c4d';
        var str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        var password = $.md5(str);

        $.ajax({
            url: "/action/login",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify({
                telephone: $("#mobile").val(),
                password: password
            }),
            success: function(resp) {
                if (resp.code == 200) {
                    window.location.href = "/goods/list";
                } else {
                    $.msg(resp.message);
                    console.log(resp.message);
                }
            },
            error: function() {
                console.log("error.");
            }
        });
    }

    function reset() {
        alert("reset");
    }
</script>
</body>
</html>